package com.example.chartview

import android.content.Context
import android.graphics.*
import android.support.v4.view.GestureDetectorCompat
import android.util.AttributeSet
import android.util.Log
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View

class YearChartMeView(context: Context, attributeSet: AttributeSet) : View(context, attributeSet) {
    private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val rectW = 40F
    private var viewWidth = 0
    private var rectOffset = 0F
    private val textRect = Rect()
    private val textYOffset = 10F
    private var list = mutableListOf<YearChartEntity>()
    private val topOffset = 200F
    private val kingOffset = 30F
    private val showCount = 5
    private val allCount = 5

    private val gestureDetectorCompat: GestureDetectorCompat =
        GestureDetectorCompat(context, object : GestureDetector.SimpleOnGestureListener() {
            override fun onDown(e: MotionEvent?): Boolean = true
            override fun onScroll(
                e1: MotionEvent?,
                e2: MotionEvent?,

                distanceX: Float,
                distanceY: Float
            ): Boolean {
                scrollBy(distanceX.toInt(), 0)
                //  判断临界值
                if (scrollX < 0) {
                    scrollX = 0
                }
                if (scrollX > viewWidth - width) {
                    scrollX = viewWidth - width
                }
                return false
            }
        })
    lateinit var linearGradient: LinearGradient
//    private val bitmap: Bitmap = getBitmapByWidth(R.mipmap.huangguan, 30 * 2)

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        viewWidth = width * allCount / showCount
        rectOffset = viewWidth / allCount * 0.5F
        linearGradient = LinearGradient(
            0F,
            0F,
            rectW,
            height.toFloat(),
            Color.parseColor("#66A6FF"),
            Color.parseColor("#364EFF"),
            Shader.TileMode.MIRROR
        )
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
//        var pos = 0
        val bottomN = height.toFloat() - 50F
        for (index in list.indices) {
            val leftN = viewWidth / allCount * index + rectOffset
            var max = rectW / 2 - bottomN +60
            var singleHight = max *list[index].number
            Log.d("singleHight",singleHight.toString())
            val topN = bottomN + singleHight.toFloat()
            val rightN = viewWidth / allCount * index + rectW + rectOffset

            paint.shader = linearGradient
            //  画长方形
            canvas.drawRect(leftN, topN, rightN, bottomN, paint)
//            canvas.drawCircle(leftN + (rightN - leftN) / 2, topN, (rightN - leftN) / 2, paint)
            canvas.drawArc(leftN, topN - 20, rightN, topN + 20, 180f, 180f, true, paint)
            //  在7上画个数字 66
            if (list[index].number == 1.00f) {
//                val bitmapX = leftN + ((rectW - bitmap.width) / 2F)
//                canvas.drawBitmap(bitmap, bitmapX, topN - kingOffset - bitmap.height, paint)
//


            }



            //  画底部数字
            paint.apply {
                val text = list[index].year
                shader = null
                color = context.resources.getColor(R.color.blue)
                textSize = 35F
                textAlign = Paint.Align.CENTER
                getTextBounds(text, 0, text.length, textRect)
                canvas.drawText(
                    text,
                    leftN + rectW / 2,
                    bottomN + textRect.height() + textYOffset,
                    paint
                )
                canvas.drawText(list[index].value,leftN + rectW / 2 , topN - kingOffset ,paint)
                println("【IM】SingleView2  onDraw: ${textRect.height()}")
            }

        }
        //  底线
        paint.color =  context.resources.getColor(R.color.blue)
        canvas.drawLine(0F, bottomN, viewWidth.toFloat(), bottomN, paint)

    }

    override fun onTouchEvent(event: MotionEvent?): Boolean =
        gestureDetectorCompat.onTouchEvent(event)

    private fun getBitmapByWidth(res: Int, width: Int): Bitmap {
        val option = BitmapFactory.Options()
        option.inJustDecodeBounds = true
        BitmapFactory.decodeResource(resources, res, option)
        option.inJustDecodeBounds = false
        option.inDensity = option.outWidth
        option.inTargetDensity = width
        return BitmapFactory.decodeResource(resources, res, option)
    }

    fun setList(listData: MutableList<YearChartEntity>) {
        this.list = listData
        invalidate()
    }
}